<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  admin_activity.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-19.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Admin_pm extends CI_Controller {

	 function __construct()
	 {
		parent::__construct();
 		$this->load->model('common/security_model');
		if($this->security_model->isAdminLogin() === FALSE){
          showmessage('请登录', 'admin/login/index', 3);
		}
 		$this->load->model('pm_model');
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
	 }
	
	function index(){
		$data = array();
		$folder = 'inbox';
		$filter = 'announcepm';
		$status = 0;
		//page
		$_SGLOBAL = $this->config->item('SGLOBAL');
		$_SGLOBAL['multipage_config']['base_url'] = base_url().'pm/index/';
		$_SGLOBAL['multipage_config']['uri_segment'] = '3';
		$_SGLOBAL['multipage_config']['per_page'] = '20';
		$offset = $this->uri->segment($_SGLOBAL['multipage_config']['uri_segment']);
		$pmdata = $this->pm_model->get_pm_list($this->user['uid'], 20, $folder, $filter, '0', $_SGLOBAL['multipage_config']['per_page']);
		if ($pmdata) {
			foreach ($pmdata['list'] as $key=>$value) {
				//实名转化
				realname_set($value['msgfromid'], $value['msgfrom']);
				$pmdata['list'][$key] = $value;
			}
				//实名转化
			realname_get();
			//page
			$_SGLOBAL['multipage_config']['total_rows'] = $pmdata['count'];//debug
			$this->pagination->initialize($_SGLOBAL['multipage_config']);
			$multi = $this->pagination->create_links();
			$data['multi'] = $multi;
			$data['list'] = $pmdata['list'];
		}
		$this->load->template('admin/admin_pm_tpl', $data);
	}

	function sendpm()
	{
		if (submitcheck('sendpmsubmit')) {
			$subject = $this->input->post('subject', true);
			$message = $this->input->post('message', true);
			$dispalyname = intval($this->input->post('dispalyname'));
			if (empty($subject) || empty($message)) {
				showmessage('标题或内容没有填写', 'admin/admin_pm/index', 3);
			}
			//显示发信人
			if ($dispalyname) {
				$senduser = array('username'=> $this->user['name'], 'uid'=>$this->user['uid']);
			}else{
				$senduser = array('username'=>'', 'uid'=>'0');				
			}
			
			// 短消息的列表又根据时间来过滤长度。
			// msgfrom 空 msgfromid, msgtoid 都为 0 为公共消息
			// msgfrom 空 msgfromid = 0 ,msgtoid 不为0 为系统消息
			$pmid = $this->pm_model->sendpm($subject, $message, $senduser, '0');
			if ($pmid) {
				showmessage('发送成功', 'admin/admin_pm/index', 3);
			}else{
				showmessage('发送失败', 'admin/admin_pm/index', 3);
			}
		}
		$data = array();
		$this->load->template('admin/admin_sendpm_tpl', $data);
	}
	
	function delete(){
		if (submitcheck('listsubmit')) {
			$ids = $this->input->post('ids', true);
			if ($ids) {
				$del = $this->pm_model->deletepm('0', $ids);
				if ($del) {
					showmessage('删除成功', 'admin/admin_pm/index', 3);
				}else{
					showmessage('删除失败', 'admin/admin_pm/index', 3);
				}
			}
		}
		showmessage('未选择消息', 'admin/admin_pm/index', 3);
	}
}
?>